using System;
using System.Net;
using System.IO;
using System.Text;

public static class ItemService
{
    public static void UploadItems(string baseUrl, string appkey)
    {
        string token = "b9K6wazu4EYnvWcKTKx7VLvJfakG"; // 示例token
        string echostr = "1597994379751"; // 示例随机数
        string sign = "SByOC98oOvC4+QygKucN3HMdd5Y="; // 示例签名
        string itemJson = @"
        {
            ""itemList"": [
                {
                    ""unitPriceStr"": ""51"",
                    ""name"": ""段关海包"",
                    ""status"": ""officia velit deserunt veniam culpa"",
                    ""sku"": ""esse Lorem ex"",
                    ""itemDetail"": ""officia cupidatat in laborum adipisicing"",
                    ""supplierPartId"": ""68"",
                    ""brandId"": ""83"",
                    ""storeCondition"": ""culpa ipsum et labore"",
                    ""desc"": ""in proident"",
                    ""cycle"": ""proident occaecat dolor aliqua ut"",
                    ""extImageUrl"": ""http://dummyimage.com/400x400"",
                    ""specifications"": ""sunt minim in enim nulla"",
                    ""purity"": ""minim ad"",
                    ""cas"": ""ut velit proident"",
                    ""id"": ""96"",
                    ""catId"": ""78""
                }
            ]
        }";

        string url = $"{baseUrl.Trim()}/supplier-openapi/v2/items/add.lf?appkey={Uri.EscapeDataString(appkey)}&token={Uri.EscapeDataString(token)}&echostr={Uri.EscapeDataString(echostr)}&sign={Uri.EscapeDataString(sign)}";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/json";

        using (var stream = request.GetRequestStream())
        using (var writer = new StreamWriter(stream, Encoding.UTF8))
        {
            writer.Write(itemJson);
            writer.Flush();
            writer.Close();
        }

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                Console.WriteLine("商品上传结果返回：");
                Console.WriteLine(result);
            }
        }
        catch (WebException e)
        {
            Console.WriteLine("发生错误：");
            Console.WriteLine(e.Message);
        }
    }
}